package com.ngm.tea.t.service.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.ngm.tea.common.PageInfo;
import com.ngm.tea.t.dao.AccountRoleDao;
import com.ngm.tea.t.entity.AccountRole;
import com.ngm.tea.t.service.AccountRoleService;

/**
 * 服务层
 * @author zjw
 * @version 1.0
 * @date 2016-09-01 21:07:07
 *
 */
@Service("accountRoleService")
public class AccountRoleServiceImpl implements AccountRoleService {
	
	@Resource
	private AccountRoleDao accountRoleDao;

	public AccountRoleDao getAccountRoleDao() {
		return accountRoleDao;
	}

	@Override
	public void insert(AccountRole accountRole) {
		accountRoleDao.insert(accountRole);
	}

	@Override
	public void deleteById(Long id) {
		accountRoleDao.deleteById(id);
	}

	@Override
	public void deleteByIds(String ids) {
		String[] _ids = ids.split(",");
		if (_ids.length > 0) {
			List<String> idlist = Arrays.asList(_ids);
			accountRoleDao.deleteByIds(idlist);
		}
	}

	@Override
	public void delete(AccountRole accountRole) {
		accountRoleDao.delete(accountRole);
	}

	@Override
	public void update(AccountRole accountRole) {
		accountRoleDao.update(accountRole);
	}

	@Override
	public AccountRole selectOne(Long id) {
		return accountRoleDao.selectOne(id);
	}

	@Override
	public List<AccountRole> selectAll() {
		return accountRoleDao.selectAll();
	}

	@Override
	public List<AccountRole> selectList(AccountRole accountRole, PageInfo pageInfo) {
		Map map = new HashMap<String, Object>();
		map.put("searchParams",makeStr(accountRole));
		map.put("pageInfo", pageInfo);
		map.put("order", pageInfo.getOrderField()+" "+pageInfo.getOrderDirection());
		return accountRoleDao.selectList(map);
	}

	@Override
	public Long count(AccountRole accountRole) {
		Map map = new HashMap<String, Object>();
		map.put("searchParams",makeStr(accountRole));
		return accountRoleDao.count(map);
	}
	
	public String makeStr(AccountRole accountRole) {
		StringBuffer sb = new StringBuffer();
		if(!StringUtils.isEmpty(accountRole.getId())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("id LIKE '%" + accountRole.getId()+"%'");
			} else {
				sb.append(" AND id LIKE '%" + accountRole.getId()+"%'");
			}
		}
		if(!StringUtils.isEmpty(accountRole.getRoleCode())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("roleCode LIKE '%" + accountRole.getRoleCode()+"%'");
			} else {
				sb.append(" AND roleCode LIKE '%" + accountRole.getRoleCode()+"%'");
			}
		}
		if (StringUtils.isEmpty(sb.toString())) {
			return null;
		} else {
			return sb.toString();
		}
	}

	@Override
	public void deleteByAccCode(String accCode) {
		Map map = new HashMap<String, Object>();
		map.put("accCode",accCode);
		accountRoleDao.deleteByAccCode(map);
	}

	@Override
	public AccountRole selectOneByAccCode(String accCode) {
		Map map = new HashMap<String, Object>();
		map.put("accCode",accCode);
		return accountRoleDao.selectOneByAccCode(map);
	}
	
}